package com.sptpc.speaker;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class NumbersActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
    AudioManager audioManager;
    AudioManager.OnAudioFocusChangeListener focusChangeListener =
            new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusState) {
            if(focusState == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
                    focusState == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                mediaPlayer.pause();
                mediaPlayer.seekTo(0);
            } else if(focusState == audioManager.AUDIOFOCUS_GAIN) {
                mediaPlayer.start();
            } else if(focusState == audioManager.AUDIOFOCUS_LOSS) {
                releseMediaPlayer();
            }
        }
    };

    public void releseMediaPlayer() {
        if(mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
            audioManager.abandonAudioFocus(focusChangeListener);
        }
    }

    @Override
    public void onDestroy() {
        releseMediaPlayer();
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);
        WordAdapter adapter = new WordAdapter(
                NumbersActivity.this,
                getWords()
        );
        RecyclerView recyclerView = findViewById(R.id.categoty_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        // 创建AudioManager
        audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    }

    /**
     * 数字页面对应的数据列表
     */
    private List<Word> getWords() {
        List<Word> words = new ArrayList<Word>();
        words.add(new Word(
                R.drawable.number_one, "一","One", R.raw.number01));
        words.add(new Word(
                R.drawable.number_two, "二","Two", R.raw.number02));
        words.add(new Word(
                R.drawable.number_three, "三","three", R.raw.number03));
        words.add(new Word(
                R.drawable.number_four, "四","four", R.raw.number04));
        words.add(new Word(
                R.drawable.number_five, "五","five", R.raw.number05));
        words.add(new Word(
                R.drawable.number_six, "六","six", R.raw.number06));
        words.add(new Word(
                R.drawable.number_seven, "七","seven", R.raw.number07));
        words.add(new Word(
                R.drawable.number_eight, "八","eight", R.raw.number08));
        words.add(new Word(
                R.drawable.number_nine, "九","nine", R.raw.number09));
        words.add(new Word(
                R.drawable.number_ten, "十","ten", R.raw.number10));
        return words;
    }
}